package cn.dam.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import cn.dam.api.entity.UserEntity;
import cn.dam.base.ResponseBase;
import cn.dam.constants.Constants;
import cn.dam.feign.MemberServiceFeign;
@Controller
public class RegisterController {
	private static final String REGISTER="register";
	private static final String LOGIN="login";
	@Autowired
	private MemberServiceFeign memberServiceFeign;
	
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String  register() {
		return REGISTER;
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String  register(UserEntity userEntity,	RedirectAttributes redirect) {
		//验证参数
		userEntity.setCreated(new Date());
		userEntity.setUpdated(new Date());
		//调用会员服务注册接口
		ResponseBase regUser = memberServiceFeign.regUser(userEntity);
		//如果失败，跳转到失败页面
		if(!regUser.getCode().equals(Constants.HTTP_RES_CODE_200)) {
			redirect.addFlashAttribute("error", "注册失败");
			return REGISTER;
		}
		//跳转到登录页面
		return LOGIN;
	}

}
